//////////////////////////////////////////////////////////////////////////
// $Id: TerrainPanel.cpp,v 1.6 2009/01/11 12:06:43 sunjun Exp $
//////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "TerrainPanel.h"

//////////////////////////////////////////////////////////////////////////

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//////////////////////////////////////////////////////////////////////////
// TerrainPanel
//////////////////////////////////////////////////////////////////////////

BEGIN_MESSAGE_MAP(TerrainPanel, CWnd)	
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_SIZE()
	ON_COMMAND_RANGE(ID_TOOL_TERRAIN_BRUSH_RAISE, ID_TOOL_TERRAIN_BRUSH_HOLE, OnToolBar)
	ON_UPDATE_COMMAND_UI_RANGE(ID_TOOL_TERRAIN_BRUSH_RAISE, ID_TOOL_TERRAIN_BRUSH_HOLE, OnUpdateToolBar)
END_MESSAGE_MAP()

TerrainPanel::TerrainPanel()
{
	
}

TerrainPanel::~TerrainPanel()
{

}

int TerrainPanel::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1) return -1;

	// create the tool bar
	{
		mToolBar.Create(this, AFX_DEFAULT_TOOLBAR_STYLE, IDT_TERRAIN_PANEL_BAR);
		mToolBar.SetFont(CFont::FromHandle((HFONT)GetStockObject(DEFAULT_GUI_FONT)));
		mToolBar.LoadToolBar(IDT_TERRAIN_PANEL_BAR, 0, 0, TRUE);
		mToolBar.CleanUpLockedImages();
		mToolBar.LoadBitmap(IDB_TERRAIN_PANEL_BAR, 0, 0, TRUE);
		mToolBar.SetPaneStyle(mToolBar.GetPaneStyle()|CBRS_TOOLTIPS|CBRS_FLYBY);
		mToolBar.SetPaneStyle(mToolBar.GetPaneStyle()&~(CBRS_GRIPPER|CBRS_SIZE_DYNAMIC));
		mToolBar.SetOwner(this);	
		mToolBar.SetRouteCommandsViaFrame(FALSE);		
	}

	// create the splitter window
	{
		CRect rect_client;
		GetClientRect(rect_client);
		int toolbar_cy = mToolBar.CalcFixedLayout(FALSE, TRUE).cy;
		int splitter_cx = rect_client.right-rect_client.left;
		int splitter_cy = rect_client.bottom-(rect_client.top+toolbar_cy);
		if( !mSplitterWnd.CreateStatic(this, 3, 1) ) return -1;
		CCreateContext* pContext = (CCreateContext*)lpCreateStruct->lpCreateParams;
		if( !mSplitterWnd.CreateView(0, 0, RUNTIME_CLASS(BrushSettingView), CSize(splitter_cx, splitter_cy/3), pContext) ) return -1;
		if( !mSplitterWnd.CreateView(1, 0, RUNTIME_CLASS(MaterialShell), CSize(splitter_cx, splitter_cy/3), pContext) ) return -1;
		if( !mSplitterWnd.CreateView(2, 0, RUNTIME_CLASS(MaterialView), CSize(splitter_cx, splitter_cy/3), pContext) ) return -1;
	}	

	return 0;
}

void TerrainPanel::OnDestroy()
{
	CWnd::OnDestroy();
	
	mSplitterWnd.DestroyWindow();
}

void TerrainPanel::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);

	CRect rect_client;
	GetClientRect(rect_client);
	int toolbar_cy = mToolBar.CalcFixedLayout(FALSE, TRUE).cy;
	mToolBar.SetWindowPos(NULL, rect_client.left, rect_client.top, rect_client.Width(), toolbar_cy, SWP_NOACTIVATE|SWP_NOZORDER);
	mSplitterWnd.SetWindowPos(NULL, rect_client.left, rect_client.top+toolbar_cy, rect_client.Width(), rect_client.bottom-(rect_client.top+toolbar_cy), SWP_NOACTIVATE|SWP_NOZORDER);
	mSplitterWnd.RecalcLayout();
}

void TerrainPanel::OnToolBar(UINT id)
{
	gToolType = id;
}

void TerrainPanel::OnUpdateToolBar(CCmdUI* pCmdUI)
{
	pCmdUI->SetCheck (pCmdUI->m_nID == gToolType);
}

//////////////////////////////////////////////////////////////////////////
// BrushSettingView
//////////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNCREATE(BrushSettingView, CFormView)
BEGIN_MESSAGE_MAP(BrushSettingView, CFormView)
	ON_WM_CREATE()
	ON_WM_SIZE()
END_MESSAGE_MAP()

BrushSettingView::BrushSettingView() : CFormView(BrushSettingView::IDD)
{

}

BrushSettingView::~BrushSettingView()
{
}

void BrushSettingView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
}

int BrushSettingView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFormView::OnCreate(lpCreateStruct) == -1) return -1;

	return 0;
}

void BrushSettingView::OnSize(UINT nType, int cx, int cy)
{
	CFormView::OnSize(nType, cx, cy);

	CRect rect_wnd;
	GetWindowRect(rect_wnd);

	// move the item
	{
		CRect rect_item;
		//	GetClientRect(rect_wnd);
		//	if(GetDlgItem(IDC_STATIC_BRUSH_SETTING))GetDlgItem(IDC_STATIC_BRUSH_SETTING)->SetWindowPos(NULL, rect_wnd.left, rect_wnd.top, rect_wnd.Width(), rect_wnd.Height(), SWP_NOACTIVATE|SWP_NOZORDER);
		if(GetDlgItem(IDC_SLIDER_RADIUS))
		{
			GetDlgItem(IDC_SLIDER_RADIUS)->GetWindowRect(rect_item);
			GetDlgItem(IDC_SLIDER_RADIUS)->MoveWindow(rect_item.left-rect_wnd.left, rect_item.top-rect_wnd.top, rect_wnd.Width()-5, rect_item.Height(), TRUE);
		}
		if(GetDlgItem(IDC_SLIDER_SOFTNESS))
		{
			GetDlgItem(IDC_SLIDER_SOFTNESS)->GetWindowRect(rect_item);		
			GetDlgItem(IDC_SLIDER_SOFTNESS)->MoveWindow(rect_item.left-rect_wnd.left, rect_item.top-rect_wnd.top, rect_wnd.Width()-5, rect_item.Height(), TRUE);
		}
		if(GetDlgItem(IDC_SLIDER_STRENGTH))
		{
			GetDlgItem(IDC_SLIDER_STRENGTH)->GetWindowRect(rect_item);		
			GetDlgItem(IDC_SLIDER_STRENGTH)->MoveWindow(rect_item.left-rect_wnd.left, rect_item.top-rect_wnd.top, rect_wnd.Width()-5, rect_item.Height(), TRUE);
		}
		//	if(GetDlgItem(IDC_SLIDER_RADIUS))GetDlgItem(IDC_SLIDER_RADIUS)->SetWindowPos(NULL, rect_wnd.left, rect_wnd.top, rect_wnd.Width(), rect_wnd.Height(), SWP_NOACTIVATE|SWP_NOZORDER);
		//	if(GetDlgItem(IDC_SLIDER_RADIUS))GetDlgItem(IDC_SLIDER_RADIUS)->SetWindowPos(NULL, rect_wnd.left, rect_wnd.top, rect_wnd.Width(), rect_wnd.Height(), SWP_NOACTIVATE|SWP_NOZORDER);
	}
}

/////////////////////////////////////////////////////////////////////////////
// MaterialShell
//////////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNCREATE(MaterialShell, CWnd)
BEGIN_MESSAGE_MAP(MaterialShell, CWnd)
	ON_WM_CREATE()
	ON_WM_SIZE()
END_MESSAGE_MAP()

MaterialShell::MaterialShell()
{

}

MaterialShell::~MaterialShell()
{

}

int MaterialShell::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1) return -1;	

	if(!mShellTreeCtrl.Create(WS_CHILD|WS_VISIBLE|TVS_HASLINES|TVS_LINESATROOT|TVS_HASBUTTONS,
		CRect(0, 0, 0, 0), this, ID_TERRAIN_MATERIAL_SHELL)) return -1;
	mShellTreeCtrl.SetFont(CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT)));

	return 0;
}

void MaterialShell::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);

	mShellTreeCtrl.SetWindowPos (NULL, -1, -1, cx, cy, SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
}

/////////////////////////////////////////////////////////////////////////////
// MaterialView
//////////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNCREATE(MaterialView, CWnd)
BEGIN_MESSAGE_MAP(MaterialView, CWnd)
	ON_WM_CREATE()
	ON_WM_SIZE()
END_MESSAGE_MAP()

MaterialView::MaterialView()
{

}

MaterialView::~MaterialView()
{

}

int MaterialView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1) return -1;	

	CRect rect_client;
	GetClientRect(rect_client);
	if( !mListCtrl.Create (WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_HSCROLL|LVS_ICON, rect_client, this, ID_TERRAIN_MATERIAL_VIEW) ) return -1;
	mListCtrl.SetFont(CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT)));

	/*
	lvitem.mask=LVIF_IMAGE|LVIF_TEXT;   
	lvitem.iItem=i;   
	lvitem.pszText="hello";   
	lvitem.iImage=i;   
	lvitem.iSubItem=0;   
	InsertItem(&lvitem); 
	*/

	return 0;
}

void MaterialView::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);
	mListCtrl.SetWindowPos (NULL, -1, -1, cx, cy, SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
}

//////////////////////////////////////////////////////////////////////////



